package main;

import grasshopper.gcm.facade.CompanyFacade;
import grasshopper.gcm.db.Company;
import grasshopper.gcm.db.CompanyAddress;
import static grasshopper.gcm.db.CompanyAddress.AddressType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import static grasshopper.utils.TextTools.convertToAscii;

/**
 *
 * @author piokob
 */
public class NewClass {

    public static void main(String[] args) throws Exception {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(GCMConstants.gcmPU);
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

        Company tp = CompanyFacade.createNewTradingPartner("PL", "pl");
        tp.setName("Syncron Poland sp. z o.o.");
        tp.setRegistrationNumber("0000274035");
        tp.setVatNumber("5252375742");

        // get legal
        if (tp.getAddresses().contains(new CompanyAddress(tp, AddressType.Legal))) {
            CompanyAddress legal = tp.getAddresses()
                .get(tp.getAddresses().indexOf(new CompanyAddress(tp, AddressType.Legal)));
            legal.setAddress1("Świętokrzyska 36/37");
            legal.setZip("00-116");
            legal.setCity("Warszawa");
            legal.setPhone("0226200289");
            legal.setFax("0226523874");

            CompanyAddress legalEn = new CompanyAddress(tp, AddressType.Legal);
            legalEn.setLanguage("en");
            legalEn.setCountry(legal.getCountry());
            legalEn.setAddress1(convertToAscii(legal.getAddress1()));
            legalEn.setAddress2(convertToAscii(legal.getAddress2()));
            legalEn.setZip("00-116");
            legalEn.setCity(convertToAscii(legal.getCity()));

            tp.getAddresses().add(legalEn);
        } else {
            throw new RuntimeException("No legal address");
        }
        em.persist(tp);

        em.getTransaction().commit();
        em.close();
        emf.close();

    }
}
